package com.yenlien.traceyun.plant.service;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.dao.*;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.plant.controller.vo.NewHomeDTO;
import com.yenlien.traceyun.plant.dao.CompanyDao;
import com.yenlien.traceyun.plant.dao.PlantDao;
import com.yenlien.traceyun.plant.dao.ReapDetailDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Date 2022/8/15
 * @Author jiaLong
 * @Descript
 */
@Service
public class DataService {
    @Autowired
    private PlantDao plantDao;

    @Autowired
    private ReapDetailDao reapDetailDao;

    @Autowired
    private ProcessDetailDao processDetailDao;

    @Autowired
    private SubpackageDao subpackageDao;

    @Autowired
    private ClientOrderDao clientOrderDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private EquipmentDao equipmentDao;

    @Autowired
    private BaseSoilDao baseSoilDao;

    public static DecimalFormat decimalFormat = new DecimalFormat("###.##");

    public List<JSONObject> getAllCompany(String companyId) {
        if(StringUtils.isBlank(companyId)){
            companyId = UserAuthUtil.getLoginUser().getCompanyId();
        }
        return companyDao.getPlantComp(companyId);
    }

    public List<JSONObject> getBaseSoil(String companyId) {
        if(StringUtils.isBlank(companyId)){
            companyId = UserAuthUtil.getLoginUser().getCompanyId();
        }
        return baseSoilDao.getByCompanyId(companyId);
    }

    public List<JSONObject> getDev(String baseSoilId) {
        return equipmentDao.findEquipment(baseSoilId);
    }


    public JSONObject count(String companyId) {
        if(StringUtils.isBlank(companyId)){
            companyId = UserAuthUtil.getLoginUser().getCompanyId();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("plantNum", plantDao.plantNumCount(companyId));
        jsonObject.put("reapNum", reapDetailDao.reapNumCount(companyId));
        jsonObject.put("ProdNum", processDetailDao.prodNumCount(companyId));
        jsonObject.put("codeNum", subpackageDao.codeNumCount(companyId));
        jsonObject.put("orderNum", clientOrderDao.orderNumCount(companyId));
        return jsonObject;
    }

    public List<JSONObject> getHistogramData(NewHomeDTO newHomeDTO) {
        if(StringUtils.isBlank(newHomeDTO.getCompanyId())){
            newHomeDTO.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        List<JSONObject> result = new ArrayList<>();
        LocalDate startTime = LocalDate.parse(newHomeDTO.getStartTime()).with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endTime = LocalDate.parse(newHomeDTO.getEndTime()).with(TemporalAdjusters.lastDayOfMonth());
        Stream.iterate(startTime, d -> d.plusMonths(1)).limit(ChronoUnit.MONTHS.between(startTime, endTime) + 1).forEach(f -> {
            JSONObject json = new JSONObject();
            String month = f.toString().substring(0, 7);
            if (StringUtils.isBlank(newHomeDTO.getName())) {
//                switch (newHomeDTO.getType()){
//                    case "种植" : json.put(month, plantDao.getTotalNum(month, newHomeDTO.getCompanyId())); break;
//                    case "采收" : json.put(month, plantDao.getTotalNum(month, newHomeDTO.getCompanyId())); break;
//                    case "成品" : json.put(month, plantDao.getTotalNum(month, newHomeDTO.getCompanyId())); break;
//                    case "赋码" : json.put(month, plantDao.getTotalNum(month, newHomeDTO.getCompanyId())); break;
//                }
                if("种植".equals(newHomeDTO.getType())) json.put(month, decimalFormat.format(plantDao.getTotalNum(month, newHomeDTO.getCompanyId())));
                if("采收".equals(newHomeDTO.getType())) json.put(month, decimalFormat.format(reapDetailDao.getTotalNum(month, newHomeDTO.getCompanyId())));
                if("成品".equals(newHomeDTO.getType())) json.put(month, decimalFormat.format(processDetailDao.getTotalNum(month, newHomeDTO.getCompanyId())));
                if("赋码".equals(newHomeDTO.getType())) json.put(month, decimalFormat.format(subpackageDao.getTotalNum(month, newHomeDTO.getCompanyId())));
                result.add(json);
            } else {
                if("种植".equals(newHomeDTO.getType())) json.put(month, decimalFormat.format(plantDao.getTotalNum(month, newHomeDTO.getCompanyId(), newHomeDTO.getName())));
                if("采收".equals(newHomeDTO.getType())) json.put(month, decimalFormat.format(reapDetailDao.getTotalNum(month, newHomeDTO.getCompanyId(), newHomeDTO.getName())));
                if("成品".equals(newHomeDTO.getType())) json.put(month, decimalFormat.format(processDetailDao.getTotalNum(month, newHomeDTO.getCompanyId(), newHomeDTO.getName())));
                if("赋码".equals(newHomeDTO.getType())) json.put(month, decimalFormat.format(subpackageDao.getTotalNum(month, newHomeDTO.getCompanyId(), newHomeDTO.getName())));
                result.add(json);
            }
        });
        return result;
    }


    public Object getPieData(NewHomeDTO newHomeDTO) {
        if(StringUtils.isBlank(newHomeDTO.getCompanyId())){
            newHomeDTO.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        if("种植".equals(newHomeDTO.getType())) return plantDao.getPercent(newHomeDTO.getCompanyId(), newHomeDTO.getStartTime(), newHomeDTO.getEndTime());
        if("采收".equals(newHomeDTO.getType())) return reapDetailDao.getPercent(newHomeDTO.getCompanyId(), newHomeDTO.getStartTime(), newHomeDTO.getEndTime());
        if("成品".equals(newHomeDTO.getType())) return processDetailDao.getPercent(newHomeDTO.getCompanyId(), newHomeDTO.getStartTime(), newHomeDTO.getEndTime());
        if("赋码".equals(newHomeDTO.getType())) return subpackageDao.getPercent(newHomeDTO.getCompanyId(), newHomeDTO.getStartTime(), newHomeDTO.getEndTime());
        return null;
    }







}
